﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;

using Common;

using Castle.Core.Interceptor;
using Castle.DynamicProxy;

namespace com.globalwave.website
{
    /// <summary>
    /// Bulletin 的摘要说明
    /// </summary>
    public class BulletinService : BaseService
    {
        private static NameValueCollection whereMap = null;
        private static string TABLE_NAME = "website_Bulletin";
        //private static string VIEW_BULLETIN_LIST = "website_bulletin_list";
        //private static string TABLE_BULLETIN = "website_Bulletin";
        //private static string TABLE_TOPIC = "website_Topic";

        public BulletinService()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public BulletinService(BaseService other)
            : base(other) 
        {
        }
        virtual public DataSet query(
                NameValueCollection criterion,
                int pageIndex,
                int pageSize,
             string orderString)
        {

            return dao.query(BulletinCriterion.LIST_NOT_CONTENT, pageIndex, pageSize, criterion, orderString == null ? " order by bulletin_id desc " : orderString, "Bulletin");
        }

        virtual public DataSet get(NameValueCollection criterion)
        {

            return dao.queryTable(TABLE_NAME, -1, -1, criterion, null);
        }

        virtual public int create(NameValueCollection bulletin)
        {
            return dao.insertTable(TABLE_NAME, bulletin);
        }

        virtual public int update(NameValueCollection bulletin)
        {
            NameValueCollection criterion = new NameValueCollection();
            string id = bulletin.Get("bulletin_id");

            criterion.Add("bulletin_id", id);

            bulletin.Remove("bulletin_id");

            int result = dao.updateTable(TABLE_NAME, bulletin, criterion);

            bulletin.Add("bulletin_id", id);

            return result;
        }

        virtual public int delete(NameValueCollection criterion)
        {
            if (criterion.Get("bulletin_id") != null)
            {
                throw new BaseException("E000001", "参数错误");
            }
            // delete cascade 
            //dao.deleteTable(TABLE_BULLETIN, criterion);
            //dao.deleteTable(TABLE_TOPIC, criterion);

            return dao.deleteTable(TABLE_NAME, criterion);

        }

        virtual public void deleteAll(string[] bulletinIds)
        {
            if (bulletinIds == null || bulletinIds.Length == 0)
            {
                throw new BaseException("", "parames");
            }

            NameValueCollection criterion = new NameValueCollection();
            foreach (string bulletinId in bulletinIds)
            {
                criterion.Add("bulletin_ids", bulletinId);
            }

            // delete cascade
            dao.deleteTable(TABLE_NAME, criterion);
            //dao.deleteTable(TABLE_TOPIC, criterion);

            //dao.deleteTable(TABLE_NAME, criterion);

        }

        /*
        virtual public DataSet getAllByUserId(string user_id)
        {
            NameValueCollection criterion = new NameValueCollection();
            criterion.Add("@user_id", user_id);

            return dao.query(bulletinCriterion.USER_ALL_bulletin, -1, criterion, null);
        }


        virtual public DataSet possessedByUser(string user_id)
        {
            NameValueCollection criterion = new NameValueCollection();
            criterion.Add("@user_id", user_id);

            return dao.query(bulletinCriterion.bulletin_PROSESSED_BY_USER, -1, criterion, null);
        }

        virtual public DataSet possessedByOrganization(string organizationId)
        {
            NameValueCollection criterion = new NameValueCollection();
            criterion.Add("@bulletin_id", organizationId);

            return dao.query(bulletinCriterion.bulletin_PROSESSED_BY_ORGANIZATION, -1, criterion, null);
        }
        */

        public NameValueCollection getWhereConfigMap()
        {
            if (whereMap != null)
            {
                return whereMap;
            }

            whereMap = new NameValueCollection();

            whereMap.Add("bulletin_ids", " bulletin_id in (@bulletin_ids) ");

            return whereMap;
        }

        public static BulletinService getInstance()
        {

            ProxyGenerator generator = new ProxyGenerator();

            return generator.CreateClassProxy<BulletinService>(new TransactionInterceptor());

        }
    }


    public class BulletinCriterion
    {
        public const String LIST_NOT_CONTENT =
                         " SELECT bulletin_id," +
                        " pro_bulletin_id," +
                        " topic_id," +
                        " topic_title," +
                        " board_id," +
                        " code_," +
                        " create_time," +
                        " update_time," +
                        " create_by," +
                        " user_name," +
                        " length_," +
                        " level_," +
                        " order_," +
                        " is_best," +
                        " ip_," +
                        " status_" +
                        " FROM website_bulletin" +
                        " WHERE pro_bulletin_id is null ";


    }
}